var ns;
var app = new Vue({
    el: '#app',
    data: {
        ID: 0,
        list: [],
        select_id_list: [],
        showchoose: false,
        showsearch:false,
        chooseList: [],
        form:{},
        isopen:false,
    },
    methods: {
        doHideSearch: function () {
            var that = this;
            that.showchoose = false;
            that.showsearch = false;
        },
        showChooseAll: function () {
            var that = this;
            that.showchoose = !that.showchoose;
            that.showsearch = !that.showsearch;//底部阴影
        },
        doAllChoosUser:function(){
            var that = this;
            that.chooseList=[];
            that.isopen=!that.isopen;
            if(that.list.length>0){
                for(let itemc of that.list){
                    itemc.isselected=that.isopen;
                }
            }
            if(that.isopen){
                if(that.list.length>0){
                    for(let itemc of that.list){
                        that.chooseList.push(itemc); 
                    }
                }
            }
        },
        doChoosUser: function (item) {
            var that = this; 
            item.isselected = !item.isselected;
            if (item.isselected) {
                that.chooseList.push(item);
                //添加
                if(that.chooseList.length==that.list.length){
                    that.isopen=true;
                }
            }
            else {
                that.isopen=false;//去掉全选
                const index = that.chooseList.findIndex(p => p.UserID === item.UserID);
                that.chooseList.splice(index, 1);
            }
            const new_list = [...new Set(that.chooseList)];// 去重
            that.chooseList = new_list;
        },
        get_data: function () {
            var that = this;
            var options = {
                DeliveryID: that.ID,
                action: "selectdeliverymanlist"
            };
            ns.post(options, function (succeed, data, err) {  
                console.log(JSON.stringify(data));
                if (data != null) {
                    that.list = data.DeliveryManList;
                    that.form = data.DeliveryInfo;
                    if (that.form.AppointStartTime && that.form.AppointEndTime) {
                        var moment1 = moment(new Date(that.form.AppointStartTime), 'MM/D/YYYY');
                        var moment2 = moment(new Date(that.form.AppointEndTime), 'MM/D/YYYY');
                        var IsSameDay = moment1.isSame(moment2, 'day');
                        that.form.AppointStartTime=moment(new Date(that.form.AppointStartTime)).format("YYYY-MM-DD HH:mm");
                        that.form.AppointEndTime=moment(new Date(that.form.AppointEndTime)).format("YYYY-MM-DD HH:mm")
                        if (IsSameDay) {
                            that.form.AppointEndTime = ns.DateFormatYY(that.form.AppointEndTime);
                        }
                    }
                }
            }, {
                toast: true
            });
        },
        set_select_items: function () {
            var that = this;
            that.select_id_list = [];
            for (var i = 0; i < that.chooseList.length; i++) {
                var item = that.chooseList[i];
                that.select_id_list.push(item.UserID);
            }
        },
        do_save: function () {
            var that = this;
            that.set_select_items();
            if (that.select_id_list.length == 0) {
                api.toast({
                    msg: '请选择配送人员',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }

            var options = {};
            options.action = "senddelivery";
            options.ID = that.ID;
            options.DeliveryManIDList = JSON.stringify(that.select_id_list);
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        api.toast({
                            msg: '操作成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    else {
                        api.toast({
                            msg: data.Msg == undefined ? '操作失败' : data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    api.sendEvent({
                        name: 'reloadorderlist',
                    });
                } else if (err) {
                    api.toast({
                        msg: err == undefined ? '接口失败' : err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                setTimeout(function () {
                    api.closeWin();
                }, 1000)
            }, {
                toast: true,
                toastmsg: '提交中'
            });
            return;
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.ID = api.pageParam.id || 0;
    app.get_data();
};